﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using 示例中体现的ModelBus核心思想.ModelBus;
using 示例中体现的ModelBus核心思想.Models.Domain;
using 示例中体现的ModelBus核心思想.Models.View;
using 示例中体现的ModelBus核心思想.Services;

namespace 示例中体现的ModelBus核心思想.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ProductsController : ControllerBase
    {
        private readonly IProductService _productService;
        private readonly ModelBusCoordinator _modelBus;

        // 注入业务服务和ModelBus协调器
        public ProductsController(IProductService productService, ModelBusCoordinator modelBus)
        {
            _productService = productService;
            _modelBus = modelBus;
        }

        /// <summary>
        /// 获取所有商品
        /// </summary>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<ProductView>>> GetAll()
        {
            // 1. 调用业务服务获取领域模型
            var domainModels = await _productService.GetAllAsync();

            // 2. 通过ModelBus转换为视图模型
            var viewModels = _modelBus.Convert<Product, ProductView>(domainModels);
            return Ok(viewModels);
        }

        /// <summary>
        /// 获取单个商品
        /// </summary>
        [HttpGet("{id}")]
        public async Task<ActionResult<ProductView>> GetById(Guid id)
        {
            var domainModel = await _productService.GetByIdAsync(id);
            if (domainModel == null)
                return NotFound();

            var viewModel = _modelBus.Convert<Product, ProductView>(domainModel);
            return Ok(viewModel);
        }

        /// <summary>
        /// 创建商品
        /// </summary>
        [HttpPost]
        public async Task<ActionResult<ProductView>> Create(ProductView viewModel)
        {
            // 视图模型转换为领域模型（此处简化，实际可能需要单独的请求模型）
            var domainModel = new Product
            {
                Id = Guid.NewGuid(),
                Name = viewModel.Name,
                Price = viewModel.Price,
                Status = ProductStatus.Draft // 默认状态
            };

            var createdDomain = await _productService.CreateAsync(domainModel);
            var createdView = _modelBus.Convert<Product, ProductView>(createdDomain);

            return CreatedAtAction(nameof(GetById), new { id = createdView.Id }, createdView);
        }

        /// <summary>
        /// 更新商品
        /// </summary>
        [HttpPut("{id}")]
        public async Task<IActionResult> Update(Guid id, ProductView viewModel)
        {
            if (id != viewModel.Id)
                return BadRequest();

            var domainModel = new Product
            {
                Id = id,
                Name = viewModel.Name,
                Price = viewModel.Price,
                // 实际应用中需要根据StatusText转换为领域枚举
                Status = ProductStatus.Active
            };

            try
            {
                await _productService.UpdateAsync(domainModel);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }

            return NoContent();
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(Guid id)
        {
            await _productService.DeleteAsync(id);
            return NoContent();
        }
    }
}
